﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace UCenter.ContactService.Email {
    public class EmailYahooService : IEmailService{
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public virtual string EmailSystem {
            get { return "yahoo.com"; }
        }

        public string StartUrl {
            get { return "https://login.yahoo.com/config/mail?.intl=us&rl=1"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("utf-8"); }
        }
        #endregion

        #region Events

        #endregion
      

        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            CookieContainer cc = new CookieContainer();
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.cn.yahoo.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            string html = WebOpHelper.GetHtml(StartUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string tries = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".tries\" value=\"", "\">");
            string src = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".src\" value=\"", "\">");
            string md5 = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".md5\" value=\"", "\">");
            string hash = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".hash\" value=\"", "\">");
            string js = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".js\" value=\"", "\">");
            string last = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".last\" value=\"", "\">");
            string promo = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\"promo\" value=\"", "\">");
            string intl = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".intl\" value=\"", "\">");
            string bypass = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".bypass\" value=\"", "\">");
            string partner = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".partner\" value=\"", "\">");
            string u = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".u\" value=\"", "\">");
            string v = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".v\" value=\"", "\">");

            string challenge = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".challenge\" value=\"", "\">");
            string yplus = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".yplus\" value=\"", "\">");
            string emailCode = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".emailCode\" value=\"", "\">");
            string pkg = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\"pkg\" value=\"", "\">");
            string stepid = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\"stepid\" value=\"", "\">");

            string ev = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".ev\" value=\"", "\">");
            string hasMsgr = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\"hasMsgr\" value=\"", "\">");
            string chkP = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".chkP\" value=\"", "\">");
            string done = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".done\" value=\"", "\">");
            string pd = WebOpHelper.Resove(html, "<input type=\"hidden\" name=\".pd\" value=\"", "\">");


            string url = "https://login.yahoo.com/config/login?";

            var nvc = new NameValueCollection();
            nvc.Add(".tries", tries);
            nvc.Add(".src", src);
            nvc.Add(".md5", md5);
            nvc.Add(".hash", hash);
            nvc.Add(".js", js);
            nvc.Add(".last",last);
            nvc.Add("promo", promo);
            nvc.Add(".partner", partner);
            nvc.Add(".u", u);
            nvc.Add(".v", v);
            nvc.Add(".challenge",challenge);
            nvc.Add(".yplus", yplus);
            nvc.Add(".emailCode", emailCode);
            nvc.Add("pkg", pkg);
            nvc.Add("stepid", stepid);
            nvc.Add(".ev", ev);
            nvc.Add("hasMsgr", hasMsgr);
            nvc.Add(".chkP",chkP);
            nvc.Add(".done", done);
            nvc.Add(".pd", pd);
            nvc.Add("login", userName+"@"+EmailSystem);
            nvc.Add("passwd", pwd);
            nvc.Add(".save", "Sign In");    
            var postContent = WebOpHelper.GetNameValueRowData(nvc,'&');
            html = WebOpHelper.Post(url, accept, contentType, refer, language, postContent, cc, DefaultEncoding, out returnUrl);
            string nextUrl = WebOpHelper.Resove(html, "<meta http-equiv=\"Refresh\" content=\"0; url=", "\">");
            html = WebOpHelper.GetHtml(nextUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            nextUrl = WebOpHelper.ResoveReverse(html, ">Go to Yahoo! Mail Classic</a>", "<a href='");
            nextUrl = "http://"+new Uri(returnUrl).Host + nextUrl;
            html = WebOpHelper.GetHtml(nextUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string addressUrl = "http://address.mail.yahoo.com/index.php?VPC=contact_list&.rand="+WebOpHelper.GetJavaScriptTime();
            html = WebOpHelper.GetHtml(addressUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            return YahooResover.Resove(html);            
        }
    }
}
